package com.bo.lock.reentrantLock;

import java.util.concurrent.locks.LockSupport;

/**
 * @Author: gpb
 * @Date: 2024/8/29 16:56
 * @Description:
 */
public class LockSupportT {
    /**
     * 测试LockSupport
     * interrupt标记为true就会被唤醒
     * interrupt标记为false不会被唤醒
     *
     * @param args
     * @throws Exception
     */
    public static void main (String[] args) throws Exception {
        Thread t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " park");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + " unpark" + ", " + Thread.currentThread().isInterrupted());
        }, "t1");
        Thread t2 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " park");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + " unpark" + ", " + Thread.currentThread().isInterrupted());
            System.out.println("1==========");
            for (int i = 0; i < 1000000000; i++) {
            }
            System.out.println("2==========");
            System.out.println(Thread.currentThread().getName() + " unpark" + ", " +Thread.interrupted());
            System.out.println(Thread.currentThread().getName() + " unpark" + ", " +Thread.interrupted());
        }, "t2");
        t1.start();
        t2.start();
        Thread.sleep(10000);
        LockSupport.unpark(t1);
        Thread.sleep(1000);
        // interrupt标记为true
        t2.interrupt();
        System.out.println("t2 执行了");

//        System.out.println(Thread.currentThread().isInterrupted());
//        // 执行interrupt之后，在打印信息
//        Thread.currentThread().interrupt();
//        // interrupt标记为true
//        System.out.println(Thread.currentThread().isInterrupted());
//        // 返回当前线程，并清除interrupt标记，标记true
//        System.out.println(Thread.interrupted());
//        // 归位false
//        System.out.println(Thread.interrupted());
//        // ==========================
//        Thread t = new Thread(()->{
//            while (!Thread.currentThread().isInterrupted()){
//                System.out.println("Thread is running");
//            }
//            System.out.println("Thread is stop");
//        });
//        t.start();
//        Thread.sleep(500);
//        t.interrupt();
    }
}
